package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerLinearViewItemDecoration;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerViewRefreshListener;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.MyRecyclerView.RefreshRecyclerView;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.commonclass.WebActivity;
import com.lishe.lishe.lishe.module.excitation.adapter.MineWelFareAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.WelfareObject;
import com.lishe.lishe.lishe.module.excitation.presenter.ExcitationPresenter;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.retrofit.BaseDataPageBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shino on 2017/11/23.
 * 我的福利
 */

public class MineWelfareActivity extends BaseActivity implements View.OnClickListener {

    private Button callBack;
    private TextView title;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private TextView empatyText;
    private RelativeLayout empatyLayout;

    private List<WelfareObject> welfareObjects = new ArrayList<>();
    private MineWelFareAdapter adapter;
    private int pageNo = 1;
    private int pageSize = 10;
    private int totalPage = 0;

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, MineWelfareActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_welfare);
        adapter = new MineWelFareAdapter(MineWelfareActivity.this, welfareObjects);
        initView();
    }

    private void initView() {
        callBack = findViewById(R.id.title_left_button);
        title = findViewById(R.id.title_text_title);
        title.setText("我的福利");
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshlayout);
        empatyLayout = findViewById(R.id.empty_layout);
        empatyText = findViewById(R.id.empty_text);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerLinearViewItemDecoration(Utils.dip2px(this, 10), false));
        recyclerView.setAdapter(adapter);
        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshLayout.setLoadmoreFinished(false);
                pageNo = 1;
                totalPage = 0;
                welfareObjects.clear();
                adapter.notifyDataSetChanged();
                getWelfareList();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (pageNo <= totalPage) {
                    refreshLayout.setLoadmoreFinished(false);
                    getWelfareList();
                } else {
                    refreshlayout.finishLoadmore();
                    refreshLayout.setLoadmoreFinished(true);
                }
            }
        });

        adapter.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
                if (welfareObjects.get(position).getCardUrl() != null & welfareObjects.get(position).getReceiveFlag().equals("0")) {
                    finish();
                    WebActivity.jump(MineWelfareActivity.this, welfareObjects.get(position).getCardUrl());
                } else {
                    WelfareDetailsActivity.jump(MineWelfareActivity.this, welfareObjects.get(position).getEmpId());
                }
            }
        });
        refreshLayout.autoRefresh();
        setListener();
    }

    private void setListener() {
        callBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_button:
                finish();
                break;
        }
    }

    /**
     * 获取福利列表
     */
    private void getWelfareList() {
        try {
            ExcitationPresenter.getInstance().getUserWelfareList(Globals.getToken(), String.valueOf(pageNo), String.valueOf(pageSize), new DataResponseListener<BaseDataPageBody>() {
                @Override
                public void onSuccess(BaseDataPageBody dataResponse) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadmore();
                    pageNo = pageNo + 1;
                    totalPage = dataResponse.getTotalPage();
                    welfareObjects.addAll(dataResponse.getInfo());
                    adapter.notifyDataSetChanged();
                    if (dataResponse.getInfo() == null || dataResponse.getInfo().size() == 0) {
                        empatyLayout.setVisibility(View.VISIBLE);
                        empatyText.setText("还没有福利哦！");
                    } else {
                        empatyLayout.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onFail(String failMessage) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadmore();
                    toast(failMessage);
                    empatyLayout.setVisibility(View.VISIBLE);
                    empatyText.setText("加载失败！");
                }

                @Override
                public void onError(Throwable e) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadmore();
                    toast("服务器或网络异常！");
                    empatyLayout.setVisibility(View.VISIBLE);
                    empatyText.setText("加载失败！");
                }

                @Override
                public void onNetWorkError() {
                    super.onNetWorkError();
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadmore();
                    Log.e("onError", "log");
                }
            });
        } catch (Exception e) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadmore();
            e.printStackTrace();
        }
    }
}
